<?php
require_once 'core/utils/TFile.inc';
require_once 'nOrm/TNOrmAdapter.inc';
require_once 'models/meta.inc';

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table catalog(cid)
 * @orm.key Id
 * @orm.nestedset Parent SubCategories
 * 
 * @property TMSCatalogCategory $Parent
 */
class TMSCatalogCategory extends TNOrmObject {
/**
 * @var int
 * @orm.scalar cid 
 */	
	public $Id;
/**
 * @var string
 * @orm.scalar name
 */	
	public $Name;
	
	protected $_ns_parent_;
	
	public $SubCategories = array();
	
	public function __toString(){
		return (string)$this->Name;
	}
}

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table unavailable_state(id)
 * @orm.key Id
 */
class TMSProductState extends TNOrmObject {
/**
 * @var int
 * @orm.scalar id
 */	
	public $Id;
/**
 * @var string
 * @orm.scalar name
 */	
	public $Name;
	
	public function __toString(){
		return (string)$this->Name;
	}
	
	public static function AvailableState(){
		$result = new TMSProductState();
		$result->Name = 'Available';
		return $result; 
	}
	
	public static function ReservedState(){
		$result = new TMSProductState();
		$result->Name = 'Reserved';
		return $result; 
	} 
	
	public static function UnavailableState(){
		$result = new TMSProductState();
		$result->Name = 'Unavailable';
		return $result;
	}
}


/**
 * @author dkrasilnikov@gmail.com
 * @orm.table nomenclature(nom_id)
 * @orm.key Id
 *
 * @property TMSCatalogCategory $Category {@orm.reference Id(nomenclature.catalog)}
 * @property TMSProductState $UnavailableState {@orm.reference Id(nomenclature.unavailable)}
 * @property TMSProductImage $Images {@orm.collection Nomenclature(nomenclature.nom_id) -s OrderNumber(asc)}
 * @property TMSArticle $Articles {@orm.collection Nomenclature(nomenclature.nom_id) -s Article(asc)}
 * @property TImage $Cover
 * @property TImage $BackCover
 * @property TImage $Preview
 * @property TImage $Icon
 */
class TMSNomenclature extends TNOrmObject {
	const PRICE_ADD_KIT = 'DEFLT';
	const PRICE_IGNORE_KIT = 'IGNKT';
	const PRICE_ONLY_KIT = 'ONLKT';
	
/**
 * @var int
 * @orm.scalar nomenclature.nom_id
 */
	public $Id;
	
/**
 * @var string
 * @orm.scalar nomenclature.name
 */	
	public $Name;
		
/**
 * @var string
 * @orm.scalar nomenclature.article_class
 */	
	public $ArticleClass;

/**
 * @var date
 * @orm.scalar nomenclature.added
 */	
	public $Added;
	
/**
 * @var float
 * @orm.scalar nomenclature.price
 */
	public $Price;
	
/**
 * @var float
 * @orm.scalar nomenclature.kit_price
 */
	public $KitPrice;
	
/**
 * @var TMSProductState
 */
	protected $_orm_unavailable_state_;

/**
 * @var TMSCatalogCategory
 */	
	protected $_orm_category_;
	
/**
 * @var TMSProductImage[]
 */
	protected $_orm_images_;
	
/**
 * @var TMSArticle[]
 */
	protected $_orm_articles_;
	
/**
 * @var bool
 * @orm.scalar allow_custom_kit
 */
	public $AllowCustomAssembly;

/**
 * @var string
 * @orm.scalar nomenclature.price_logic
 */	
	public $PriceLogic;
		
	public function __toString(){
		return (string)$this->Name;
	}	
}


/**
 * @author dkrasilnikov@gmail.com
 * @orm.table article(artid)
 * @orm.key Id
 *
 * @property TMSNomenclature $Nomenclature {@orm.reference Id(article.nomenclature)} 
 * @property string $State
 * @property bool $AllowOrder  
 */
class TMSArticle extends TNOrmObject {
/**
 * @var int
 * @orm.scalar article.artid 
 */	
	public $Id;
	
/**
 * @var string
 * @orm.scalar article.article
 */	
	public $Article;
	
/**
 * @var float
 * @orm.scalar article.price
 */	
	public $Price;

/**
 * @var float
 * @orm.scalar article.kit_price
 */
	public $KitPrice;
		
/**
 * @var date
 * @orm.scalar article.added
 */	
	public $Added;
	
/**
 * @var TMSNomenclature
 */	
	protected $_orm_nomenclature_;
		
	public function __toString(){
		return (string)(($this->Article?($this->Article.' - '):'').$this->Nomenclature);
	}	
		
	public function __get($nm){
		switch ($nm){
			case 'State':return (string)TMSProductState::AvailableState();break;
			case 'AllowOrder':return true;break;
			default:return parent::__get($nm);break;
		}
	}
}

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table goods(goodsid)
 */
class TMSGoods extends TMSArticle {
	
/**
 * @var int
 * @orm.scalar goods.quantaty
 */	
	public $Quantaty;

/**
 * @var int
 * @orm.scalar goods.reserved
 */	
	public $Reserved;
	
	public function __get($nm){
		switch ($nm){
			case 'State':{
				if ($this->Quantaty == 0){
					if ($this->Nomenclature->UnavailableState)
						return (string)$this->Nomenclature->UnavailableState;
					else
						return (string)TMSProductState::UnavailableState();
				} else if (($this->Quantaty - $this->Reserved) <= 0)
					return 	(string)TMSProductState::ReservedState();
				return (string)TMSProductState::AvailableState();
			}break;
			case 'AllowOrder':return ($this->Quantaty > 0) && ($this->Quantaty > $this->Reserved);break;
			default:return parent::__get($nm);break;
		}
	}
}

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table service(service_id)
 *
 */
class TMSService extends TMSArticle {

/**
 * @var bool
 * @orm.scalar service.active
 */
	public $Active;
	
	public function __get($nm){
		switch ($nm){
			case 'State':{
				return $this->Active?((string)TMSProductState::AvailableState()):((string)TMSProductState::UnavailableState());	
			}break;
			case 'AllowOrder':return $this->Active;break;
			default:return parent::__get($nm);break;
		}
	}
}

/**
 * @author dkrasilnikov@gmail.com
 * @orm.table catalog_image(imid)
 * @orm.key Id 
 *
 * @property TMSNomenclature $Nomenclature {@orm.reference Id(nomenclature)} 
 */
class TMSProductImage extends TNOrmObject {
/**
 * @var int
 * @orm.scalar imid
 */	
	public $Id;
	
/**
 * @var int
 * @orm.scalar ordernum
 */	
	public $OrderNumber;
/**
 * @var TMSNomenclature
 */	
	protected $_orm_nomenclature_;
/**
 * @var string
 * @orm.scalar description
 */	
	public $Description;
	
	public function __toString(){
		return (string)$this->Description;
	}	
}